package com.fa.wisdom.ui;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.coder.zzq.smartshow.dialog.LoadingDialog;
import com.fa.wisdom.R;
import com.fa.wisdom.adapter.DeviceAdapter;
import com.fa.wisdom.base.Constant;
import com.fa.wisdom.base.LazyLoadBaseFragment;
import com.fa.wisdom.model.DeviceEntity;
import com.fa.wisdom.model.ResultEntity;
import com.fa.wisdom.net.MyCallback;
import com.fa.wisdom.net.Task;
import com.fa.wisdom.utils.GridItemDecoration;
import com.fa.wisdom.utils.Loading;
import com.fa.wisdom.utils.SpUtils;
import com.tuya.smart.android.demo.camera.CameraPanelActivity;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import okhttp3.Call;

import static com.tuya.smart.android.demo.camera.utils.Constants.INTENT_DEV_ID;

public class PublicFragment extends LazyLoadBaseFragment {
    private int pid;
    private TextView tv_detail;
    private RecyclerView recycle;
    private DeviceAdapter adapter;
    public int position;
    List<DeviceEntity> newsList = new ArrayList<>();
    GridItemDecoration divider;

    public PublicFragment(int id, int position) {
        this.pid = id;
        this.position = position;
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.fragment_public;
    }

    @Override
    protected void initView(View rootView) {
        tv_detail = rootView.findViewById(R.id.tv_detail);
        recycle = rootView.findViewById(R.id.recycle);
        adapter = new DeviceAdapter(newsList);
        recycle.setHasFixedSize(true);
        recycle.setNestedScrollingEnabled(false);
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        recycle.setLayoutManager(staggeredGridLayoutManager);
        if (divider == null)
            divider = new GridItemDecoration.Builder(requireActivity())
                    .setHorizontalSpan(R.dimen.common_vew_column_padding)
                    .setVerticalSpan(R.dimen.common_vew_raw_padding)
                    .setColorResource(R.color.transparent)
                    .setShowLastLine(false)
                    .build();
        recycle.addItemDecoration(divider);
        recycle.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
               String name="";
                switch (newsList.get(position).getId()){
                    case 22://定位手环
                        name="定位手环";
                        startActivity(new Intent(requireActivity(),PublicActivity.class)
                                .putExtra("name",name));
                        break;
                    case 2://摄像机
                        name="摄像机";
                        Intent intent = new Intent(requireActivity(), CameraPanelActivity.class);
                        intent.putExtra(INTENT_DEV_ID, "6c8ebce276d1b5d15eggcw");
                        startActivity(intent);
                        break;
                    case 17://红外
                        name="红外";
                        startActivity(new Intent(requireActivity(),PublicActivity.class)
                                .putExtra("name",name));
                        break;
                    case 16://求救
                        name="求救";
                        startActivity(new Intent(requireActivity(),PublicActivity.class)
                                .putExtra("name",name));
                        break;
//            case 22://定位手环
//                break;
//            case ://摄像机
//                break;
//            case 22://定位手环
//                break;
//            case ://摄像机
//                break;
                }
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (recycle!=null&&divider!=null)
            recycle.removeItemDecoration(divider);
    }

    @Override
    protected void requestData() {
        super.requestData();
        if (position == 0)
            getList();
    }

    @Override
    protected void requestDataAutoRefresh() {
        super.requestDataAutoRefresh();
        getList();
    }

    //获取对应的
    private void getList() {

        LoadingDialog dialog = Loading.getInstance().onShowLargeLoading("努力加载中...");
        dialog.showInActivity(requireActivity());
        int userID = SpUtils.getInstance().decodeInt(Constant.User_id, -1);
        String token = SpUtils.getInstance().decodeString(Constant.TOKEN, "");

        OkHttpUtils.get().url("https://br.feiaikeji.cn/index.php/api/device/get_device_lists")
                .addParams("device_group_id", String.valueOf(pid))
                .addParams("user_id", String.valueOf(userID))
                .addParams("token", token).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                dialog.dismiss();
            }

            @Override
            public void onResponse(String response, int id) {
                dialog.dismiss();
                JSONObject jsonObject = JSON.parseObject(response);
                JSONArray jsonArray = jsonObject.getJSONArray("data");
                if (jsonArray != null && jsonArray.size() > 0) {
                    newsList.clear();
                    for (int i = 0; i < jsonArray.size(); i++) {
                        JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                        DeviceEntity deviceEntity = new DeviceEntity();
                        deviceEntity.setId(jsonObject1.getInteger("type"));
                        deviceEntity.setName(jsonObject1.getString("device_name"));
                        deviceEntity.setCreateTime(jsonObject1.getString("update_time"));
                        newsList.add(deviceEntity);
                    }
                }
                adapter.notifyDataSetChanged();
            }
        });
    }
}
